//
// Created by 11510 on 2024/6/7.
//

#include "Computer.h"
#include <string.h>
#include <iostream>
using std::cout;
using std::endl;

Computer* Computer::_pInstance = nullptr;

/**
 * 私有构造方法
 * @param price
 * @param brand
 * @return
 */
Computer::Computer(double price, const char *brand)
        : _price(price), _brand( new char[strlen(brand) + 1]()) {
    strcpy(_brand, brand);
    cout << "Constructor" << endl;
}
/**
 * @brief 私有析构函数
 */
Computer::~Computer(){
    cout << "~Computer" << endl;
}

/**
 * @note static 定义方法时候不用加
 * 获得单例的方法
 * @return 一个单例对象的地址
 */
Computer * Computer::getInstance(){
    if(_pInstance == nullptr){
        _pInstance = new Computer(0, "huawei");
    }
    return _pInstance;
}

/**
 * 修改单例的值
 * @param price
 * @param brand
 */
void Computer:: init(double price, const char *brand){
    if(_brand){
        delete [] _brand;
    }
    _brand = new char[strlen(brand) + 1]();
    strcpy(_brand, brand);

    _price = price;
}

/**
 * @brief 打印单例的内容
 */
void Computer::toString() const {
    cout << "(" << this->_price
         << "," << this->_brand
         << ")" << endl;
}

/**
 * @brief 销毁对象的函数
 */
void Computer::destroy() {
    if(_pInstance) {
        cout << "delete" << endl;
        delete _pInstance;
        _pInstance = nullptr;
    }
}

